package cn.antblog.jdbc;

import java.sql.*;

/**
 * @program: dmc-base
 * @description: 使用jdbc排查 syntax error, unexpected $undefined   #{参数不读取}
 * @author: 杨敬一
 * @create: 2021-03-19 10:52
 **/
public class JDBCTest {


    /**
     * Statement 和 PreparedStatement之间的关系和区别.
     * 关系：PreparedStatement继承自Statement,都是接口
     * 区别：PreparedStatement可以使用占位符，是预编译的，批处理比Statement效率高
     */
    public static void conn() {
        String URL = "jdbc:mysql://10.125.193.190:50152/sdzqtest";
        String USER = "sdzqtest";
        String PASSWORD = "sdzq_TEST007";
        // 1.加载驱动程序
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.获得数据库链接
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            // 3.通过数据库的连接操作数据库，实现增删改查（使用Statement类）
            String TIMEST = "20210225";
            //预编译
            String sql = "select CITY_NAME,CUST_NAME,CONTRACT_NUMBER,ACCOUNT_ID,CHUZ_BN_HS,CHUZ_BN_BHS,CHUZ_SN_HS,CHUZ_SN_BHS,NEW_BN_SR, SETTLEMENT,ARREARAGE_BN,RZJSR_BN,QUOTA_DXM,REWARD_SUM,CREATE_STAFF_ID,CREATE_TIME,trade_id,for_id,month_id from sdzqtest.zq_dxmjl_wlwdlj_ht";
            PreparedStatement statement = conn.prepareStatement(sql);
            //statement.setString(1, TIMEST);
            ResultSet rs = statement.executeQuery();
//			String sql="select * from userinfo where UserName='"+name+"'";
//			Statement statement = conn.createStatement();
//			ResultSet rs = statement.executeQuery(sql);
            // 4.处理数据库的返回结果(使用ResultSet类)
            while (rs.next()) {
                // System.out.println(rs);
                System.out.println(rs.getString("CITY_NAME") + " " + rs.getString("CUST_NAME") + " " + rs.getString("CONTRACT_NUMBER"));
            }
            // 关闭资源
            conn.close();
            rs.close();
            statement.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        conn();
    }
}
